﻿#include "base64cfgclass.h"
#include <QRegExpValidator>

#include "qfiledialog.h"
#include "qdebug.h"
#include "base64helper.h"

Base64Cfgclass::Base64Cfgclass(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	ui.txtFile->setPlaceholderText(QString::fromUtf8(u8"请选择一张图片进行转换"));
	ui.txtText->setPlaceholderText(QString("Warning: 0\t\t\tERROR: 0"));
	//圆角边框
	ui.labImage->setStyleSheet("border: 1px groove gray; border-radius: 5px; background-color: rgba(255, 193, 245, 0%); ");
	setAcceptDrops(true);

}

Base64Cfgclass::~Base64Cfgclass()
{}


void Base64Cfgclass::showTime(qint64 size1, qint64 size2)
{
    //统计用时
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
    double elapsed = (double)timer.nsecsElapsed() / 1000000;
#else
    double elapsed = (double)timer.elapsed();
#endif
    QString time = QString::number(elapsed, 'f', 3);
    ui.labInfo->setText(QString::fromUtf8(u8"用时: %1 毫秒  大小: %2 -> %3").arg(time).arg(size1).arg(size2));
}

void Base64Cfgclass::on_btnOpen_clicked()
{
	QString fileName = QFileDialog::getOpenFileName(this, QString::fromUtf8(u8"选择文件"), "", "图片(*.png *.bmp *.jpg)");
	if (!fileName.isEmpty()) {
		ui.txtFile->setText(fileName);
		QPixmap pix(fileName);
		pix = pix.scaled(ui.labImage->size() - QSize(4, 4), Qt::KeepAspectRatio);
		ui.labImage->setPixmap(pix);
	}
}

void Base64Cfgclass::on_btnClear_clicked()
{
	ui.txtFile->clear();
	ui.txtText->clear();
	ui.txtBase64->clear();
	ui.labImage->clear();
}

void Base64Cfgclass::on_btnImageToBase64_clicked()
{
	 QString fileName = ui.txtFile->text().trimmed();
	 if (fileName.isEmpty()) {
	     return;
	 }
	 timer.restart();
	 QImage image(fileName);
	 QString text = Base64Helper::imageToBase64(image);
	 showTime(QFile(fileName).size(), text.size());
	 ui.txtBase64->setText(text);
}

void Base64Cfgclass::on_btnBase64ToImage_clicked()
{
	 QString fileName = ui.txtFile->text().trimmed();
	 QString text = ui.txtBase64->toPlainText().trimmed();
	 if (text.isEmpty()) {
	     return;
	 }

	 timer.restart();
	 QImage image = Base64Helper::base64ToImage(text);
	 showTime(text.size(), QFile(fileName).size());
	 QPixmap pix = QPixmap::fromImage(image);
	 pix = pix.scaled(ui.labImage->size() - QSize(4, 4), Qt::KeepAspectRatio);
	 ui.labImage->setPixmap(pix);
}

void Base64Cfgclass::on_btnTextToBase64_clicked()
{
	QString text = ui.txtText->text().trimmed();
	if (text.isEmpty()) {
	    return;
	}

	timer.restart();
	QString result = Base64Helper::textToBase64(text);
	showTime(text.size(), result.size());
	ui.txtBase64->setText(result);
}

void Base64Cfgclass::on_btnBase64ToText_clicked()
{
	QString text = ui.txtBase64->toPlainText().trimmed();
	if (text.isEmpty()) {
	    return;
	}

	timer.restart();
	QString result = Base64Helper::base64ToText(text);
	showTime(text.size(), result.size());
	ui.txtText->setText(result);
}

void Base64Cfgclass::dragEnterEvent(QDragEnterEvent* event) {
	QStringList acceptedFileTypes;
	acceptedFileTypes.append("jpg");
	acceptedFileTypes.append("jpeg");
	acceptedFileTypes.append("bmp");
	acceptedFileTypes.append("png");
	if (event->mimeData()->hasUrls() && event->mimeData()->urls().count() == 1) {
		QFileInfo file(event->mimeData()->urls().at(0).toLocalFile());
		if (acceptedFileTypes.contains(file.suffix().toLower())) {
			event->acceptProposedAction();//表明用户可以在窗口部件上拖放对象
		}
	}
}

void Base64Cfgclass::dropEvent(QDropEvent* event) {
	QFileInfo file(event->mimeData()->urls().at(0).toLocalFile());
	if (pixmap.load(file.absoluteFilePath())) {
		ui.labImage->setPixmap(pixmap.scaled(ui.labImage->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
		ui.txtFile->setText(file.absoluteFilePath());
	}
	else {
		QMessageBox::critical(this, tr("Error"), tr("The image file count not be read"));
	}
}


void Base64Cfgclass::resizeEvent(QResizeEvent* event) {
	Q_UNUSED(event);
	if (!pixmap.isNull()) {
		ui.labImage->setPixmap(pixmap.scaled(ui.labImage->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
	}

}



